package exercise;

import expression.AddExpression;
import expression.SubExpression;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 习题集系统类 - 实验内容2
 * 为程序提供Main入口，完成50道加法、50道减法和50道混合习题
 */
public class Main {
    public static void main(String[] args) {
        System.out.println("面向对象的软件构造 - 实验内容2");
        System.out.println("完成50道加法、50道减法和50道混合习题生成");
        System.out.println("==========================================");
        
        // 生成50道加法题
        System.out.println("\n1. 50道加法题:");
        generateAdditionExercises(50);
        
        // 生成50道减法题
        System.out.println("\n2. 50道减法题:");
        generateSubtractionExercises(50);
        
        // 生成50道混合题
        System.out.println("\n3. 50道加减法混合题:");
        generateMixedExercises(50);
        
        System.out.println("\n==========================================");
        System.out.println("所有习题生成完成！");
    }
    
    public static void generateAdditionExercises(int count) {
        // 创建自定义习题集只生成加法题
        CustomExerciseSet set = new CustomExerciseSet(count, "addition");
        set.displayQuestions();
        set.displayAnswers();
    }
    
    public static void generateSubtractionExercises(int count) {
        // 创建自定义习题集只生成减法题
        CustomExerciseSet set = new CustomExerciseSet(count, "subtraction");
        set.displayQuestions();
        set.displayAnswers();
    }
    
    public static void generateMixedExercises(int count) {
        // 创建自定义习题集生成混合题
        CustomExerciseSet set = new CustomExerciseSet(count, "mixed");
        set.displayQuestions();
        set.displayAnswers();
    }
    
    /**
     * 自定义习题集类，用于生成指定类型的题目
     */
    static class CustomExerciseSet {
        private List<Exercise> exercises;
        private String type;
        private int count;
        
        public CustomExerciseSet(int count, String type) {
            this.count = count;
            this.type = type;
            this.exercises = new ArrayList<>();
            generateExercises();
        }
        
        private void generateExercises() {
            Random random = new Random();
            
            if ("addition".equals(type)) {
                // 只生成加法题
                for (int i = 0; i < count; i++) {
                    int a = random.nextInt(71) + 30; // 30-100
                    int b = random.nextInt(71) + 30;
                    exercises.add(new Exercise(new AddExpression(a, b)));
                }
            } else if ("subtraction".equals(type)) {
                // 只生成减法题
                for (int i = 0; i < count; i++) {
                    int a = random.nextInt(71) + 30; // 30-100
                    int b = random.nextInt(71) + 30;
                    if (a < b) {
                        int temp = a;
                        a = b;
                        b = temp;
                    }
                    exercises.add(new Exercise(new SubExpression(a, b)));
                }
            } else {
                // 生成混合题
                for (int i = 0; i < count; i++) {
                    int a = random.nextInt(71) + 30;
                    int b = random.nextInt(71) + 30;
                    if (random.nextBoolean()) {
                        exercises.add(new Exercise(new AddExpression(a, b)));
                    } else {
                        if (a < b) {
                            int temp = a;
                            a = b;
                            b = temp;
                        }
                        exercises.add(new Exercise(new SubExpression(a, b)));
                    }
                }
            }
        }
        
        public void displayQuestions() {
            System.out.println("==========================================");
            System.out.println("          " + getTypeName() + " (" + exercises.size() + "道)");
            System.out.println("==========================================");
            
            for (int i = 0; i < exercises.size(); i++) {
                System.out.printf("%2d. %s\n", i + 1, exercises.get(i).getQuestion());
            }
            System.out.println("==========================================");
        }
        
        public void displayAnswers() {
            System.out.println("==========================================");
            System.out.println("         " + getTypeName() + "参考答案");
            System.out.println("==========================================");
            
            for (int i = 0; i < exercises.size(); i++) {
                System.out.printf("%2d. %s\n", i + 1, exercises.get(i).getQuestionWithAnswer());
            }
            System.out.println("==========================================");
        }
        
        private String getTypeName() {
            switch (type) {
                case "addition": return "加法题";
                case "subtraction": return "减法题";
                case "mixed": return "混合题";
                default: return "习题";
            }
        }
        
        public List<Exercise> getExercises() {
            return exercises;
        }
    }
}